package com.lungle.warmdiary.app.utils;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.widget.Toast;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 分享工具
 */

public class ShareUtils {
    private static final String WEIXIN_PKG = "com.tencent.mm";
    private static final String WEIXIN_ACTIVITY = "com.tencent.mm.ui.tools.ShareToTimeLineUI";

    private static final String WEIBO_PKG = "com.sina.weibo";


    /**
     * 不实用微信的SDK分享图片到好友
     *
     * @param context
     * @param path
     */
    public static void sharePicToFriendNoSDK(Context context, String path) {
        if (!isInstallWeChart(context, WEIBO_PKG)) {
            Toast.makeText(context, "您没有安装微信", Toast.LENGTH_SHORT).show();
            return;
        }
        Intent intent = new Intent();
        ComponentName comp = new ComponentName(WEIXIN_PKG, "com.tencent.mm.ui.tools.ShareImgUI");
        intent.setComponent(comp);
        intent.setAction("android.intent.action.SEND");
        intent.setType("image/*");
        // intent.setFlags(0x3000001);
        File f = new File(path);
        if (f.exists()) {
            intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(path)));
        } else {
            Toast.makeText(context, "文件不存在", Toast.LENGTH_SHORT).show();
            return;
        }
        context.startActivity(intent);
    }


    /**
     * 分享9图到朋友圈
     *
     * @param context
     * @param Kdescription 9图上边输入框中的文案
     * @param paths        本地图片的路径
     */
    public static void share9PicsToWXCircle(Context context, String Kdescription, List<String> paths) {
        if (!isInstallWeChart(context, WEIXIN_PKG)) {
            Toast.makeText(context, "您没有安装微信", Toast.LENGTH_SHORT).show();
            return;
        }
        Intent intent = new Intent();
        intent.setComponent(new ComponentName(WEIXIN_PKG, WEIXIN_ACTIVITY));
        intent.setAction("android.intent.action.SEND_MULTIPLE");

        ArrayList<Uri> imageList = new ArrayList<Uri>();
        for (String picPath : paths) {
            File f = new File(picPath);
            if (f.exists()) {
                imageList.add(Uri.fromFile(f));
            }
        }
        if (imageList.size() == 0) {
            Toast.makeText(context, "图片不存在", Toast.LENGTH_SHORT).show();
            return;
        }
        intent.setType("image/*");
        intent.putExtra(Intent.EXTRA_STREAM, imageList); //图片数据（支持本地图片的Uri形式）
        intent.putExtra("Kdescription", Kdescription); //微信分享页面，图片上边的描述
        context.startActivity(intent);
    }

    /**
     * 不使用微信sdk检查是否安装微信
     *
     * @param context
     * @return
     */
    public static boolean isInstallWeChart(Context context, String pkg) {
        PackageInfo packageInfo = null;
        try {
            packageInfo = context.getPackageManager().getPackageInfo(pkg, 0);
        } catch (Exception e) {
            packageInfo = null;
            e.printStackTrace();
        }
        if (packageInfo == null) {
            return false;
        } else {
            return true;
        }
    }

    public static void share9PicsToWeibo(Context context, String Kdescription, List<String> paths) {
        if (!isInstallWeChart(context, WEIBO_PKG)) {
            Toast.makeText(context, "您没有安装新浪微博", Toast.LENGTH_SHORT).show();
            return;
        }

        Intent intent = querySendIntent(context, WEIBO_PKG);
        if (intent == null)
            throw new RuntimeException("unable send to weibo");
        ArrayList<Uri> imageList = new ArrayList<Uri>();
        for (String picPath : paths) {
            File f = new File(picPath);
            if (f.exists()) {
                imageList.add(Uri.fromFile(f));
            }
        }
        if (imageList.size() == 0) {
            Toast.makeText(context, "图片不存在", Toast.LENGTH_SHORT).show();
            return;
        }
        intent.setType("image/*");
        intent.putExtra(Intent.EXTRA_STREAM, imageList); //图片数据（支持本地图片的Uri形式）
        intent.putExtra(Intent.EXTRA_TEXT, Kdescription);
        context.startActivity(intent);
    }


    private static Intent querySendIntent(Context context, String pkg) {
        Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
        intent.setPackage(pkg);
        intent.setType("image/*");
        List<ResolveInfo> resInfo = context.getPackageManager().queryIntentActivities(intent, 0);
        if (resInfo.isEmpty())
            return null;
        return intent;
    }

}
